SHELL = /bin/sh

# verify that JAVA_HOME is set to something (should be the bare minimum)
ifndef JAVA_HOME
    $(error ERROR: Variable JAVA_HOME is not set!)
endif


#
# C++ compiler options
# 
CC = g++
CFLAGS = -g
INCLUDES = -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux 
LIBS = -ljvm -L$(JAVA_HOME)/jre/lib/i386 -L$(JAVA_HOME)/jre/lib/amd64 -L$(JAVA_HOME)/jre/lib/i386/client/ -L$(JAVA_HOME)/jre/lib/amd64/server/

#
# Java compiler option
#
JCC = $(JAVA_HOME)/bin/javac
JFLAGS =

#
# GIWS options
#
GIWS = ../../giws
GFLAGS = -p

#
# GIWS PROJECT INFORMATION 
#
PACKAGE_NAME = example
OBJECT_NAME  = MyObject
BINARY       = main

GIWS_CPP_FILE  = main.cpp
GIWS_DESC_FILE = $(OBJECT_NAME).giws.xml
GIWS_OUT_FILES = $(addprefix $(PACKAGE_NAME),.cpp .hxx)


# in case of --throws-exception-on-error, add 2 files to the list
# of the ones generated by GIWS.
ifneq (,$(findstring --throws-exception-on-error,$(GFLAGS)))
	GIWS_OUT_FILES += GiwsException.cpp GiwsException.hxx
endif


#########################################################################
#########################################################################
#########################################################################

# look for sources in current folder and in package name
VPATH = .:$(PACKAGE_NAME)


all: $(OBJECT_NAME).class $(BINARY)


# build java class file
$(OBJECT_NAME).class: $(OBJECT_NAME).java
	$(JCC) $(JFLAGS) $<


# build output binary
$(BINARY): $(GIWS_OUT_FILES)
	$(CC) $(GIWS_CPP_FILE) $(GIWS_OUT_FILES) $(CFLAGS) $(LIBS) $(INCLUDES) -o $(BINARY)
	@if test $(MAKELEVEL) -eq 0; then \
		echo "==========================================================================="; \
		echo "Dont forget to set library path before running the program:"; \
		echo "# export LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(JAVA_HOME)/jre/lib/i386:$(JAVA_HOME)/jre/lib/i386/client/"; \
		echo "==========================================================================="; \
	fi


# run giws to generate cpp code
$(GIWS_OUT_FILES):
	$(GIWS) -f $(GIWS_DESC_FILE) $(GFLAGS)


# cleanup giws generated files (cpp/hxx, bin) and .class
clean:
	rm -f $(GIWS_OUT_FILES) $(PACKAGE_NAME)/$(OBJECT_NAME).class $(BINARY)

